package com.snake.springboot.authority.web.controller;

import com.snake.springboot.authority.api.model.AuthOperatorRoleModel;
import com.snake.springboot.authority.api.request.AuthorizationQuery;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.api.response.AuthOperatorRoleRsp;
import com.snake.springboot.authority.api.service.AuthorizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类 {@code AuthorizationController}  <br> .
 * <p>详细描述
 * 创建时间：2024/5/28 18:09
 *
 * @author Yzhui
 * @see
 */
@Api(tags = "权限管理-授权")
@RestController
@RequestMapping("/authority")
public class AuthorizationController {

    private AuthorizationService authorizationService;

    @Autowired
    public AuthorizationController(AuthorizationService authorizationService) {
        this.authorizationService = authorizationService;
    }

    @ApiOperation("授权")
    @PostMapping("/authorization")
    public ResponseResult<Object> authorization(
            @ApiParam(name = "操作员与角色关系列表", value = "传入json类型", required = true)
            @RequestBody List<AuthOperatorRoleModel> models) {
        authorizationService.authorization(models);
        return ResponseResultFactory.ucc("授权成功!");
    }

    @ApiOperation("取消操作员所有授权")
    @GetMapping("/cancel-authorization/{operatorid}")
    public ResponseResult<Object> cancelAuthorization(
            @ApiParam(name = "操作员ID", value = "传入String类型", required = true)
            @PathVariable("operatorid") String operatorId) {
        authorizationService.cancelAuthorization(operatorId);
        return ResponseResultFactory.ucc("操作成功!");
    }

    @ApiOperation("查询指定操作员已授权的角色")
    @GetMapping("/query/selected-roles/{operatorid}")
    public ResponseResult<List<AuthOperatorRoleRsp>> querySelectedRoles(
            @ApiParam(name = "操作员ID", value = "传入String类型", required = true)
            @PathVariable("operatorid") String operatorId) {
        List<AuthOperatorRoleRsp> result = authorizationService.queryOperatorRole(operatorId);
        return ResponseResultFactory.ucc("授权成功!", result);
    }

    @ApiOperation("查询指定操作员已授权的菜单")
    @GetMapping("/query/auth-menu/{operatorid}")
    public ResponseResult<List<AuthMenuRsp>> queryAuthMenu(
            @ApiParam(name = "操作员ID", value = "传入String类型", required = true)
            @PathVariable("operatorid") String operatorId) {
        List<AuthMenuRsp> result = authorizationService.queryAuthMenuTree(operatorId);
        return ResponseResultFactory.ucc("查询成功!", result);
    }

    @ApiOperation("查询指定操作员已授权的菜单")
    @PostMapping("/list/auth-menu")
    public ResponseResult<List<AuthMenuRsp>> queryAuthMenu(
            @ApiParam(name = "角色与菜单关系的入参", value = "传入json类型", required = true)
            @RequestBody AuthorizationQuery query) {
        List<AuthMenuRsp> result = authorizationService.queryAuthMenuTree(query);
        return ResponseResultFactory.ucc("查询成功!", result);
    }

}
